/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.employee.service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.CalendarUtils;
import com.thinkgem.jeesite.common.utils.Constons;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.employee.entity.Checkinout;
import com.thinkgem.jeesite.modules.schedul.entity.Schclass;
import com.thinkgem.jeesite.modules.schedul.entity.SchedulingInformation;
import com.thinkgem.jeesite.modules.schedul.service.SchclassService;
import com.thinkgem.jeesite.modules.schedul.service.SchedulingInformationService;
import com.thinkgem.jeesite.modules.shift.entity.Shift;
import com.thinkgem.jeesite.modules.shift.service.ShiftService;
import com.thinkgem.jeesite.modules.employee.dao.CheckinoutDao;

/**
 * 打卡记录Service
 * @author pan.zhou
 * @version 2017-07-19
 */
@Service
@Transactional(readOnly = true)
public class CheckinoutService extends CrudService<CheckinoutDao, Checkinout> {
    
    @Autowired
    private SchedulingInformationService schedulingInformationService;
    
    @Autowired
    private SchclassService schclassService;
    
    @Autowired
    private ShiftService shiftService;
    


	public Checkinout get(String id) {
		return super.get(id);
	}
	
	public List<Checkinout> findList(Checkinout checkinout) {
		return super.findList(checkinout);
	}
	
	public Page<Checkinout> findPage(Page<Checkinout> page, Checkinout checkinout) {
		return super.findPage(page, checkinout);
	}
	
	@Transactional(readOnly = false)
	public void save(Checkinout checkinout) {
		super.save(checkinout);
	}
	
	@Transactional(readOnly = false)
	public void delete(Checkinout checkinout) {
		super.delete(checkinout);
	}

	/**
	 * 统计某个员工在某段时间内的打卡记录
	 * @param employeeNumer
	 * @param starTime
	 * @param endTime
	 * @return
	 */
	public List<Checkinout> findListByeMployeeNumerAndDate(String todatStr, String employeeNumer, Date starTime, Date endTime) {

        Date sbdkStarTime = starTime;//上班打卡时间

        try {
            String sbdkStarTimeStr = todatStr + " " + CalendarUtils.formatTimeNoSecond(sbdkStarTime);
            sbdkStarTime = CalendarUtils.parseDateTimeNoSecond(sbdkStarTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date sbdkendTime = endTime;//
        try {
            String sbdkendTimeStr = todatStr + " " + CalendarUtils.formatTimeNoSecond(sbdkendTime);
            sbdkendTime = CalendarUtils.parseDateTimeNoSecond(sbdkendTimeStr);
            if (CalendarUtils.millisBetween(sbdkendTime, sbdkStarTime) <= 0) {
                sbdkendTime = CalendarUtils.addDate(sbdkendTime, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

		Checkinout checkinout = new Checkinout(employeeNumer,sbdkStarTime,sbdkendTime);
		return dao.findList(checkinout);
	}
	
	public boolean isExceptionRecords(String employeeNumer,Date today){
	    
	    SchedulingInformation schedulingInformation = new SchedulingInformation(employeeNumer,today,today,Constons.DELETE_N);
        List<SchedulingInformation> schedulingInformationList = schedulingInformationService.findList(schedulingInformation);
        //根据排班获取班次
        List<Shift> shiftList = shiftService.findAllByEmployeeNumerAndDate(schedulingInformationList, DateUtils.getWeekByDate(today));
        //根据班次获取考勤时段，根据时段计算
        if(shiftList!=null&&shiftList.size()>0){
            Schclass schclass = schclassService.get(shiftList.get(0).getSchclass().getId());
            Date sbdkStarTime = schclass.getCheckintime1();//上班打卡时间
            Date sbdkEndTime = schclass.getCheckouttime2();//下班打卡时间
            String todatStr =  CalendarUtils.formatDate(today);//变成yyyy-mm-dd

            List<Checkinout> sbdkCheckinoutList = findListByeMployeeNumerAndDate(todatStr,employeeNumer,sbdkStarTime,sbdkEndTime);
            if(sbdkCheckinoutList.size()%2==0){
                return true;
            }else{
                return false; 
            }
                                    
        
        }
        
        return true;
        
    	    
	}
}